package com.v2future.sjt.collegecheckingattendance.rollcall;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.v2future.sjt.collegecheckingattendance.MainActivity;
import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.home.HomeActivity;
import com.v2future.sjt.collegecheckingattendance.rollcall.adapter.SubjectAdapter;
import com.v2future.sjt.collegecheckingattendance.rollcall.bean.SubjectBean;
import com.v2future.sjt.collegecheckingattendance.utils.BaseResponseBean;
import com.v2future.sjt.collegecheckingattendance.utils.Constants;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.BaseRequestParams;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetTaskListener;
import com.v2future.sjt.collegecheckingattendance.utils.netutils.NetUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by minnie on 16/1/20.
 * 选择科目
 */
public class SelectSubjectActivity extends MainActivity implements AdapterView.OnItemClickListener {

    ListView subjectsLv;
    SubjectAdapter adapter;
    List<SubjectBean> datalist;
    private SubjectBean currentSelect;

    /**
     * 当前课程标识
     */
    public static final String INTENT_TAG_CURRENTCOURSE = "current_course";

    @Override
    public void init() {
        currentSelect = (SubjectBean) getIntent().getSerializableExtra(INTENT_TAG_CURRENTCOURSE);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_subject_select);
        initTitleBack(new onBackClick());
        setTitleText(getString(R.string.select_subject_name));
        subjectsLv = (ListView) findViewById(R.id.lv_subjects);
    }

    @Override
    public void initAfter() {
        subjectsLv.setAdapter(adapter);
        subjectsLv.setOnItemClickListener(this);
        doGetTeacherCourse();
    }

    public void refreshView(){
        //添加选中项
        if (null != currentSelect) {
            for (int i=0;i<datalist.size();i++) {
                if (currentSelect.getCourseId().equals(datalist.get(i).getCourseId())) {
                    datalist.get(i).setChecked(true);
                }
            }
        }
        adapter = new SubjectAdapter(this, datalist);
        subjectsLv.setAdapter(adapter);
    }

    public void doGetTeacherCourse(){
        BaseRequestParams params = new BaseRequestParams(this, formatUrl(Constants.TEACHE_COURSE));
        showTitleLoading();
        NetUtils.doGetRequest(this, params, new NetTaskListener<SubjectResponeBean>() {

            /**
             * 数据获取完成
             *
             * @param result
             */
            @Override
            public void onSuccess(SubjectResponeBean result) {
                datalist = result.getData();
                refreshView();
            }

            /**
             * 位置网络错误
             */
            @Override
            public void onError() {

            }

            /**
             * 非正常状态的数据
             *
             * @param result
             */
            @Override
            public void onInterError(SubjectResponeBean result) {
                tip(result.getFailReason());
            }

            /**
             * 网络错误
             *
             * @param responseCode
             * @param responseMsg
             * @param errorResult
             */
            @Override
            public void onNetError(int responseCode, String responseMsg, String errorResult) {
                tip(responseMsg);
            }

            /**
             * 请求取消
             */
            @Override
            public void onCancelled() {

            }

            /**
             * 网络请求流程结束
             */
            @Override
            public void onFinished() {
                cancelTitleLoading();
            }

            /**
             * 缓存获取完成
             *
             * @param result
             */
            @Override
            public void onSuccessCache(SubjectResponeBean result) {

            }
        });
    }

    /**
     * 返回键的按键事件
     */
    public class onBackClick implements View.OnClickListener {

        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SelectSubjectActivity.this, HomeActivity.class);
            intent.putExtra("id", HomeActivity.FLAG_ROLLCALL);//回到主页请假/点名fragment
            intent.putExtra(INTENT_TAG_CURRENTCOURSE, currentSelect);
            intent.setAction(INTENT_TAG_CURRENTCOURSE);
            gobackWithData(intent);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        for (int i=0;i<datalist.size();i++) {
            datalist.get(i).setChecked(false);
        }
        datalist.get(position).setChecked(true);
        currentSelect = datalist.get(position);
        adapter.notifyDataSetChanged();
    }

    /**
     * 教师点名课程数据
     */
    public class SubjectResponeBean extends BaseResponseBean implements Serializable {

        private List<SubjectBean> data;

        public List<SubjectBean> getData() {
            return data;
        }

        public void setData(List<SubjectBean> data) {
            this.data = data;
        }
    }
}
